<template>
  <view class="post-detail-page">
    <!-- 帖子标题 -->
    <view class="post-title">{{ detailData.name }}</view>
    <!-- 帖子信息（作者和发布时间） -->
    <view class="post-info">
      <text>作者: {{ detailData.user }}</text>
      <text>发布时间: {{ formmattedDate }}</text>
    </view>
    <!-- 帖子内容 -->
    <view class="section-title">帖子内容</view>
    <view class="post-content">{{ detailData.content }}</view>
    <!-- 帖子描述 -->
    <view class="section-title">帖子描述</view>
    <view class="post-content">{{ detailData.desc }}</view>
    <!-- 评论列表标题 -->
    <view class="comment-list-title">评论列表</view>
    <!-- 评论列表 -->
    <view class="comment-list">
      <view v-for="(comment, index) in comments" :key="index" class="comment-item">
        <view class="comment-header">
          <view class="comment-author">{{ comment.author }}</view>
          <view class="comment-date">{{ comment.date }}</view>
        </view>
        <view class="comment-content">{{ comment.content }}</view>
      </view>
    </view>
    <!-- 发表评论 -->
    <view class="comment-input-section">
      <text>发表评论</text>
      <view class="pinglun">
        <input v-model="pinglunValue" placeholder="请输入评论" class="input-field" />
        <button @click="send">发布</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { onLoad } from "@dcloudio/uni-app";
import { getTieziDetail, pinglun, addpinglun } from '../../api/luntan.js';
import dayjs from 'dayjs';

let TieId = ref('');
let detailData = ref({});
let ceartTime = ref('');
const formmattedDate = ref('');
let name = ref('');
let pinglunValue = ref('');

onLoad((option) => {
  // 获取传递参数
  console.log(option.id);
  console.log(option.name);
  name.value = option.name;
  TieId.value = option.id;
  getDetail();
  getPinglun();
});

// 获取详情
async function getDetail() {
  let r1 = await getTieziDetail(TieId.value);
  console.log(r1.data.data);
  detailData.value = r1.data.data;
  // 将创建日期转化为年月日格式
  ceartTime.value = detailData.value.createdAt;
  formmattedDate.value = dayjs(ceartTime.value).format('YYYY-MM-DD HH:mm:ss');
}

// 获取评论列表
async function getPinglun() {
  let r2 = await pinglun(TieId.value, 1, 10, name.value);
  console.log(r2.data);
}

// 发表评论
async function send() {
  console.log(pinglunValue.value);
  let r3 = await addpinglun(TieId.value, pinglunValue.value);
  console.log(r3.data);
  console.log("评论");
}

// 模拟评论数据
const comments = ref([
  {
    author: '学习者 A',
    date: '2024-11-02',
    content: '说得很有道理，我很期待 WebAssembly 在前端的更多应用。'
  },
  {
    author: '开发者 B',
    date: '2024-11-03',
    content: 'PWA 确实是个很好的方向，能提高用户留存率。'
  }
]);
</script>

<style scoped>
.post-detail-page {
  padding: 20px;
  background-color: #f9f9f9;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.post-title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
}

.post-info {
  font-size: 14px;
  color: #777;
  margin-bottom: 25px;
  display: flex;
  gap: 20px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  color: #555;
  margin-bottom: 10px;
}

.post-content {
  font-size: 16px;
  line-height: 1.7;
  color: #444;
  background-color: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
}

.comment-list-title {
  font-size: 22px;
  font-weight: bold;
  color: #333;
  margin-bottom: 15px;
  border-bottom: 2px solid #eee;
  padding-bottom: 10px;
}

.comment-list {
  margin-bottom: 30px;
}

.comment-item {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.comment-author {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.comment-date {
  font-size: 12px;
  color: #999;
}

.comment-content {
  font-size: 14px;
  line-height: 1.6;
  color: #555;
}

.comment-input-section {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 20px;
}

.comment-input-section text {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
  display: block;
}

.pinglun {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 15px;
}

.input-field {
  flex: 1;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 6px;
  font-size: 14px;
}

.pinglun button {
  padding: 12px 20px;
  background-color: #007aff;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.pinglun button:hover {
  background-color: #0056b3;
}
</style>